package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.broadcast.MyBroadcastReceiver;
import com.example.myapplication.broadcast.MyLocalBroadcastReceiver;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    private EditText edit_user;
    private EditText edit_pawd;
    private Button btn_login;
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login3);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        bindViews();
        IntentFilter filter = new IntentFilter("com.example.myapplication.MY_BROADCAST_LOCAL");
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        MyLocalBroadcastReceiver receiver = new MyLocalBroadcastReceiver();
        localBroadcastManager.registerReceiver(receiver,filter);
    }

    private void bindViews() {
        edit_user = (EditText) findViewById(R.id.username);
        edit_pawd = (EditText) findViewById(R.id.password);
        btn_login = (Button) findViewById(R.id.login);
        btn_login.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //发送应用内广播
        Intent intent = new Intent();
        intent.setAction("com.example.myapplication.MY_BROADCAST_LOCAL");
        localBroadcastManager.sendBroadcast(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(!pref.getString("user","").equals("")){
            edit_user.setText(pref.getString("user",""));
            edit_pawd.setText(pref.getString("pawd",""));
        }
    }

    @Override
    public void onClick(View v) {
        String user = edit_user.getText().toString();
        String pawd = edit_pawd.getText().toString();
        /**
         * 根据username查询密码 db操作
         */
        if(user.equals("123")&&pawd.equals("123")){
            editor = pref.edit();
            editor.putString("user", user);
            editor.putString("pawd", pawd);
            editor.commit();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"检测到账号在别处登录,请确保是本人操作！！",Toast.LENGTH_SHORT).show();
            finish();
        }else{
            Toast.makeText(LoginActivity.this,"登录失败！",Toast.LENGTH_SHORT).show();
        }

    }
}